package tk.uodrad.apex.project.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import tk.uodrad.apex.project.pojo.dto.ParkingUseAddNewDTO;
import tk.uodrad.apex.project.pojo.dto.ParkingUseUpdateDTO;
import tk.uodrad.apex.project.pojo.entity.ParkingUse;
import tk.uodrad.apex.project.pojo.vo.ParkingUseListVO;
import tk.uodrad.apex.project.service.ParkingUseService;
import tk.uodrad.apex.project.web.JsonResult;

import java.util.Arrays;

@Slf4j
@RestController
@RequestMapping("/parkingUse")
@Transactional
public class parkingUseController {
    @Autowired
    private ParkingUseService parkingUseService;

    /**
     * 添加车位使用
     * @param parkingUseAddNewDTO 添加车位使用DTO
     * */
    @PostMapping("/add")
    public JsonResult<String> add(@RequestBody ParkingUseAddNewDTO parkingUseAddNewDTO) {
        parkingUseService.save(parkingUseAddNewDTO);
        return JsonResult.ok("添加成功");
    }

    /**
     * 根据ID数组删除车位使用
     * @param ids ID数组
     * */
    @GetMapping("/delete")
    public JsonResult<String> deleteById(Long[] ids) {
        parkingUseService.removeByIds(Arrays.asList(ids));
        return JsonResult.ok("删除成功！");
    }

    /**
     * 根据ID修改车位使用信息
     * @param parkingUseUpdateDTO 修改车位使用DTO
     * */
    @PostMapping("/update")
    public JsonResult<String> update(@RequestBody ParkingUseUpdateDTO parkingUseUpdateDTO) {
        parkingUseService.updateById(parkingUseUpdateDTO);
        return JsonResult.ok("修改成功！");
    }

    /**
     * 分页查询车位使用列表
     * @param pageNum 第几页
     * @param pageSize 每页多少条数据
     * @return IPage<ParkingUse> 分页对象
     * */
    @GetMapping("/list")
    public JsonResult<IPage<ParkingUseListVO>> list(
            @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize,
            @RequestParam(value = "communityCode", required = false) String communityCode) {
        IPage<ParkingUseListVO> list = parkingUseService.list(pageNum, pageSize,communityCode);
        return JsonResult.ok(list);
    }
}
